package com.cnhaimei.vod;

import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.util.ArrayList;
import java.util.List;

import android.os.Environment;
import android.util.Log;

import com.cnhaimei.vod.bean.Song;
import com.cnhaimei.vod.util.Util;


/**
 * 私人收藏管理器 <br>
 * 
 * @author funwell <br>
 * @date 2013-7-27
 * 
 */
public class FavoriteManager {
	private final static FavoriteManager mG = new FavoriteManager();
	private FavoriteData mData = null;
	private boolean mIsLoaded = false;
	private final static String SAVE_FILE_NAME = "/favorite.dat";
	private boolean mIsDataChanged = false;

	public enum OperateResultType {
		SUCCESS, //成功
		FAIL, //失败
		EXIST_ALREADY, //已存在
	}
	
	private FavoriteManager() {
		//mData = new FavoriteData();
	}

	public static FavoriteManager G() {
		return mG;
	}
	
	public OperateResultType addSong(Song song) {
		if (mData != null && mData.getList() != null) {
			for (Song item : mData.getList()) {
				if (item.getName().equals(song.getName()) && item.getSingerName().equals(song.getSingerName())) {
					return OperateResultType.EXIST_ALREADY;
				}
			}
			mData.getList().add(song);
			mIsDataChanged = true;
			return OperateResultType.SUCCESS;
		}
		return OperateResultType.FAIL;
	}
	
	public OperateResultType insertSong(Song song, int index) {
		if (mData != null && mData.getList() != null) {
			for (Song item : mData.getList()) {
				if (item.getName().equals(song.getName()) && item.getSingerName().equals(song.getSingerName())) {
					return OperateResultType.EXIST_ALREADY;
				}
			}
			mData.getList().add(index, song);
			mIsDataChanged = true;
			return OperateResultType.SUCCESS;
		}
		return OperateResultType.FAIL;
	}
	
	public OperateResultType deleteSong(int index) {
		if (mData != null && mData.getList() != null) {
			mData.getList().remove(index);
			mIsDataChanged = true;
			return OperateResultType.SUCCESS;
		}
		return OperateResultType.FAIL;
	}
	
	public OperateResultType deleteSong(Song song) {
		if (mData != null && mData.getList() != null) {
			for (Song item : mData.getList()) {
				if (item.getName().equals(song.getName()) && item.getSingerName().equals(song.getSingerName())) {
					mData.getList().remove(item);
					mIsDataChanged = true;
					return OperateResultType.SUCCESS;
				}
			}
		}
		return OperateResultType.FAIL;
	}
	
	public List<Song> getList() {
		if (mData != null && mData.getList() != null) {
			return mData.getList();
		}
		
		return null;
	}
	
	public boolean isDataChanged() {
		return mIsDataChanged;
	}
	
	public void setDataChanged(boolean isChanged) {
		mIsDataChanged = isChanged;
	}
	
	public void save() {
		try {
			if (mData != null) {
				//创建ObjectOutputStream对象，准备序列化对象s 
				ObjectOutputStream oos = new ObjectOutputStream(
						new FileOutputStream(Environment.getExternalStorageDirectory() + SAVE_FILE_NAME)); 
				//调用writeObject将对象进行序列化，存储在文件favorite.dat中。 
				oos.writeObject(mData); 
				oos.flush(); 
				oos.close(); 
			}
		} catch (Exception e) {
			// TODO: handle exception
			Log.e("FavoriteManager", e.getMessage());
		}
	}
	
	public boolean load() {
		boolean bSuccess = true;
		try {
			//创建ObjectInputStream对象，准备从文件abc中反序列化SerialClass对象 
			ObjectInputStream ois = new ObjectInputStream(
					new FileInputStream(Environment.getExternalStorageDirectory() + SAVE_FILE_NAME)); 
			//调用readObject将存储在文件abc中的字节流反序列化为SerialClass对象。 
			mData = (FavoriteData) ois.readObject(); 
		} catch (Exception e) {
			// TODO: handle exception
			Log.e("FavoriteManager", e.getMessage());
			bSuccess = false;
		}
		
		mIsLoaded = true;
		if (mData == null) {
			mData = new FavoriteData();
			mData.setList(new ArrayList<Song>());
		}
		
		return bSuccess;
	}
	
	public boolean isLoaded() {
		return mIsLoaded;
	}
	

}
